package com.eqdd.yiqidian.ui.mine.setting;

import android.databinding.DataBindingUtil;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.yiqidian.MvcHelperTestActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.loadmore.SlimMoreLoader;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.shizhefei.mvc.MVCUltraHelper;

import java.util.ArrayList;
import java.util.List;

public class MvcHelperTestActivity extends BaseActivity {
    private MvcHelperTestActivityCustom dataBinding;
    private String workCircleId;

    private SlimAdapterEx<Demo> slimAdapterEx;
    private MVCUltraHelper<List<Demo>> mvcHelper;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_mvc_helper_test);
        initTopTitleBar(View.VISIBLE, "测试");
        initTopRightText("刷新", v -> {
            slimAdapterEx.notifyDataChanged(new ArrayList<Demo>(), true);
        });
    }

    @Override
    public void initData() {
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_01, new SlimInjector<Demo>() {
            @Override
            public void onInject(Demo data, IViewInjector injector) {

                injector.text(R.id.tv_name, data.getName());
            }
        }).attachTo(dataBinding.recyclerView);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                        System.out.println(position);
                    }
                });
//        ModelRx2DataSource<Demo> dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
//            @Override
//            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
//
//
//                return Flowable.just(demos);
//            }
//        });
        slimAdapterEx.updateData(new ArrayList<>());
//        RefreshUtil.openRefresh(dataBinding.swipeRefresh, new RefreshUtil.OnRefresh() {
//            @Override
//            public void refresh(CoolRefreshView coolRefreshView) {
//                ArrayList<Demo> demos = new ArrayList<>();
//                for (int i = 0; i < 10; i++) {
//                    demos.add(new Demo(i, i + ""));
//                }
//                slimAdapterEx.updateData(demos);
//                dataBinding.swipeRefresh.setRefreshing(false);
//            }
//        });
        slimAdapterEx.enableLoadMore(new SlimMoreLoader(this) {
            @Override
            protected void onLoadMore(Handler handler) {
                ArrayList<Demo> demos = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    demos.add(new Demo(i, i + ""));
                }
                slimAdapterEx.notifyDataChanged(demos, false);
            }

            @Override
            protected boolean hasMore() {
                return true;
            }
        });
        dataBinding.appbarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (verticalOffset >= 0) {
                    dataBinding.swipeRefresh.setEnabled(true);
                } else {
                    dataBinding.swipeRefresh.setEnabled(false);
                }
            }
        });
        dataBinding.swipeRefresh.setProgressViewOffset(true, -20, 100);
        dataBinding.swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                System.out.println(Thread.currentThread().getName());
                ArrayList<Demo> demos = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    demos.add(new Demo(i, i + ""));
                }
                slimAdapterEx.updateData(demos);
                dataBinding.swipeRefresh.setRefreshing(false);
            }
        });

    /*
     * 配置PtrClassicFrameLayout的刷新样式
     */
//        mvcHelper = new MVCUltraHelper<List<Demo>>(dataBinding.ptrFrameLayout);
//        MVCRefreshUtil.setUtrlRefreshHeader(this,dataBinding.ptrFrameLayout);
//        MVCSwipeRefreshHelper<List<Demo>> mvcSwipeRefreshHelper = new MVCSwipeRefreshHelper<>(dataBinding.swipeRefresh);
//        mvcSwipeRefreshHelper.setDataSource(dataSource);
//        mvcSwipeRefreshHelper.setAdapter(slimAdapterEx);
//        mvcSwipeRefreshHelper.refresh();

//        mvcHelper.setDataSource(dataSource);
//        mvcHelper.setAdapter(slimAdapterEx);
//        mvcHelper.refresh();
    }

    @Override
    public void setView() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    class Demo {
        int id;
        String name;

        public Demo(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getId() {

            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }


}
